#! /bin/bash

#
# backup
# ------
# Simple backup creator.
#
USAGE=" backup v0.2: \n
		usage : Change into the directory you want you backup and
				run the script. It will create a tar-file with time
				and date in the name.
				Additionally if a storage device was found with the file
				.backupstorage. the script will copy the backup to the device.
				\n\n
				parameters: 	-? this help\n
								-s ignore .svn folders\n
								-c back up only *.cpp, *.c, *.h and [Mm]akefiles\n\n"

# ToDo : Problems when whitespaces in the folder's name

if [ "$1" == "-?" ]; then
	echo -e $USAGE;
	exit;
fi

MYDATETIME=`date +"%Y%m%d_%H%M%S"`

echo "Backing up " $PWD

FolderToBackup=`basename "$PWD"`
FolderName=`basename "$PWD"`_

## Leerzeichen escapen
#FolderToBackup=$(echo "$FolderToBackup" | sed 's/ /\\ /g')
#FolderName=`echo $FolderToBackup`_

BACKUPFILE=$FolderName$MYDATETIME.tar.gz

rm -vfi ../$FolderName*tar.gz

echo $1;

if [ "$1" = "-s" ]; then
	echo "Ignoring hidden svn files ... ";
	tar cvz --exclude='.svn' --file ../$BACKUPFILE ../$FolderToBackup
elif [ "$1" = "-c" ]; then
	echo "Backing up only c, cpp, h and [Mm]akefiles ... ";
	find ../$FolderToBackup -name '*.cpp' -or -name '*.c' -or -name '*.h' -or -name '*akefile' | sed 's/\\n/ /g' | xargs tar cvzf ../$BACKUPFILE
else
	tar cvzf ../$BACKUPFILE ../$FolderToBackup
fi

echo "Backup saved to "../$BACKUPFILE

ls -alh ../$BACKUPFILE

backupdevice=`mount | awk -F" " '{print $3}'`
# Erklärung des awk - Kommandos
# -F setzt den Feldseparator: in diesen Fall ein Leerzeichen
# print $3 gibt das 3. Feld aus: hier mount-Punkte

for i in $backupdevice # test all mount points
do
	if [ -f $i/.backupstorage ]; then # if the file is found, it is my backup storage
		echo "Copying "$BACKUPFILE" to "$i
		rsync --progress ../$BACKUPFILE $i
		break;
	fi
done

